package com.zfei.gfb.util;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.zfei.gfb.api.domain.*;
import com.zfei.gfb.bean.Status;
import com.zfei.gfb.bean.StatusException;
import com.zfei.gfb.core.Conf;
import com.zfei.gfb.device.RedisNames;
import com.zfei.gfb.device.Types;
import com.zfei.gfb.model.GfbGreenPlot;
import com.zfei.gfb.model.GfbHouse;
import com.zfei.gfb.request.h5.GreenPlotClaimRequest;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author fgh
 */
public class TalkUtil {

    private static final Logger logger = LoggerFactory.getLogger(TalkUtil.class);


    public static void cover(TalkEvent event) {
        if (event == null) {
            return;
        }

        // ext
        JsonObject ext = Gsons.parseJsonObject(event.getExt());
    }

    public static TalkEvent sweep(TalkEvent event, boolean handleContent, boolean handleLocation) {
        cover(event);
        event.setExt(null);

        // content
        if (handleContent && event.isDeleted()) {
            event.setContent(null);
        }

        return event;
    }

    public static TalkEvent sweep(TalkEvent event, boolean handleContent) {
        return sweep(event, handleContent, true);
    }

    public static TalkEvent sweep(TalkEvent event) {
        return sweep(event, true);
    }

    public static User sweep(User user) {
        user.setMobile(null);
        user.setYid(null);
        user.setBits(null);
        return user;
    }


    /**
     * check event content
     *
     * @param event event
     * @return resource
     */
    public static void checkEvent(TalkEvent event) {
//        if (event == null || event.getContent() == null || event.getAccount() == null || event.getEventTime() == null
//                || event.getSource() == null) {
//            throw new StatusException(Status.INVALID_ARGUMENT.value(), "missing event, content, account ,eventtime, source");
//        }
//
//        if (event.getEventType() == null || !EventType.ALL.contains(event.getEventType())) {
//            throw new StatusException(Status.INVALID_ARGUMENT.value(), "invalid event type");
//        }
//
//        if (event.getEventTier() == null || !EventTier.ALL.contains(event.getEventTier())) {
//            throw new StatusException(Status.INVALID_ARGUMENT.value(), "invalid event tier");
//        }
//
//        int contentMaxSize = Conf.getInteger("TalkEventContentMaxSize", 20000);
//        if (event.getContent().length() > contentMaxSize) {
//            throw new StatusException(Status.INVALID_ARGUMENT.value(), "exceed max content size: " + contentMaxSize);
//        }
    }

    private static void checkEventContent(Integer eventType, Content content) {
        int textMaxSize = Conf.getInteger("TalkEventContentTextMaxSize", 5000);
        if (content.getText() != null && content.getText().length() > textMaxSize) {
            throw new StatusException(Status.INVALID_ARGUMENT.value(), "exceed max content text size: " + textMaxSize);
        }

        int mediaMaxSize = Conf.getInteger("TalkEventContentMediaMaxSize", 100);

        if (eventType == EventType.ARTICLE && StringUtils.isBlank(content.getLink())) {
            throw new StatusException(Status.INVALID_ARGUMENT.value(), "invalid event type");
        }

        if (eventType == EventType.RICHTEXT && StringUtils.isBlank(content.getRichText())) {
            throw new StatusException(Status.INVALID_ARGUMENT.value(), "invalid rick text");
        }

        //检查话题名称是否为空，如果为空不让发帖
        Map<String, Object> topics = content.getTopic();
        if (topics != null && topics.size() > 0) {
            Set<Map.Entry<String, Object>> entries = topics.entrySet();
            for (Map.Entry<String, Object> entry : entries) {
                if (StringUtils.isBlank(entry.getKey())) {
                    throw new StatusException(Status.INVALID_ARGUMENT.value(), "invalid topic name");
                }
            }
        }
    }


    public static void auditLog(JsonObject ext, String log) {
        List<String> list = Gsons.fromJson(Gsons.getJsonArray(ext, Ext.aud), Types.StringListType);
        list.add(log);

        JsonElement aud;
        int fromIndex = list.size() - Conf.getInteger("ExtAudSizeMax", 3);
        if (fromIndex > 0) {
            aud = Gsons.toJsonTree(list.subList(fromIndex, list.size()));
        } else {
            aud = Gsons.toJsonTree(list);
        }

        ext.add(Ext.aud, aud);
    }


    public static String getTalkCommentName(String eventId) {
        return RedisNames.join(InboxName.TALK_COMMENT, eventId);
    }

    public static TalkEvent buildCollect(GfbHouse house, String uid, long now) {
        TalkEvent event = new TalkEvent();
        event.setEventType(EventType.TEXT);
        event.setContent(TalkEvent.COLLECT);
        event.setSource(house.getId().toString());
        event.setParent(house.getId().toString());
        event.setOrigin(house.getId().toString());
        event.setCorrelation(null);
        event.setAccount(uid);
        event.setEventTime(now);
        event.setEventTier(EventTier.COLLECT);
        event.setPrivilege(EventPrivilege.AUDIT);
        return event;
    }

    public static TalkEvent buildBrowse(GfbHouse house, String uid) {
        TalkEvent event = new TalkEvent();
        event.setEventType(EventType.TEXT);
        event.setContent(TalkEvent.BROWSE);
        event.setSource(house.getId().toString());
        event.setParent(house.getId().toString());
        event.setOrigin(house.getId().toString());
        event.setCorrelation(null);
        event.setAccount(uid);
        event.setEventTime(Now.millis());
        event.setEventTier(EventTier.BROWSE);
        event.setPrivilege(EventPrivilege.AUDIT);
        return event;
    }

    public static TalkEvent buildClaim(GfbGreenPlot greenPlot, String uid, long now, GreenPlotClaimRequest request) {
        TalkEvent event = new TalkEvent();
        event.setEventType(EventType.TEXT);
        event.setContent(Gsons.toJson(request));
        event.setSource(greenPlot.getId().toString());
        event.setParent(greenPlot.getId().toString());
        event.setOrigin(greenPlot.getId().toString());
        event.setCorrelation(null);
        event.setAccount(uid);
        event.setEventTime(now);
        event.setEventTier(EventTier.CLAIM);
        event.setPrivilege(EventPrivilege.AUDIT);
        return event;
    }

}
